package com.anyikang.volunteer.sos.volunteer.app.mine;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.anyikang.volunteer.sos.volunteer.R;
import com.anyikang.volunteer.sos.volunteer.app.MyApplication;
import com.anyikang.volunteer.sos.volunteer.app.MyConstant;
import com.anyikang.volunteer.sos.volunteer.app.bean.EmergencyInfo;
import com.anyikang.volunteer.sos.volunteer.app.bean.MyRescueDetailData;
import com.anyikang.volunteer.sos.volunteer.app.bean.MyRescueDetailJson;
import com.anyikang.volunteer.sos.volunteer.app.bean.SosPerson;
import com.anyikang.volunteer.sos.volunteer.app.bean.User;
import com.anyikang.volunteer.sos.volunteer.app.bean.Volunteer;
import com.anyikang.volunteer.sos.volunteer.utils.CommonUtil;
import com.anyikang.volunteer.sos.volunteer.utils.MyErrorDeal;
import com.anyikang.volunteer.sos.volunteer.utils.okhttputils_callback.StringDialogCallback;
import com.google.gson.Gson;
import com.lzy.okhttputils.OkHttpUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

public class RescueDetailActivity extends AppCompatActivity {

    TextView tvVolunteerName;
    TextView tvRescueAddress;
    TextView tvSosTime;
    TextView tvRescueTime;
    TextView tvSosName0;
    TextView tvSosName;
    TextView tvSosSex;
    TextView tvSosAge;
    TextView tvSosPhone;
    TextView tvSosPersonAddress;
    TextView tvEmergencyName1;
    TextView tvEmergencyMobile1;
    TextView tvAddress1;
    TextView tvEmergencyName2;
    TextView tvEmergencyMobile2;
    TextView tvAddress2;
    ImageView imvSosPerson;
    String TAG = "RescueDetailActivity";
    private String mOrderId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rescue_detail);
        initView();
        initData();
    }

    /**
     * 处理救援详情成功
     * @param data
     */
    public void dealSuccess(MyRescueDetailData data)
    {
        if(data != null)
        {
            //救援详情。志愿者信息
            Volunteer volunteer = data.getVolunteer();
            tvVolunteerName.setText(volunteer.getName());
            tvRescueAddress.setText(volunteer.getAddress());

            long sosTime = volunteer.getSosTime();
            String strSosTime = CommonUtil.getDateTimeYmdHms(Long.parseLong(sosTime+""));
            tvSosTime.setText(strSosTime);

            long rescueTime = volunteer.getReceiveTime();
            String strRescueTime = CommonUtil.getDateTimeYmdHms(Long.parseLong(rescueTime+""));
            tvRescueTime.setText(strRescueTime);

            //求救人详细信息
            SosPerson sosPerson = data.getSosPerson();
            tvSosName0.setText(sosPerson.getName());
            tvSosName.setText(sosPerson.getName());

            String imageUrl = sosPerson.getImageUrl();
            String picUrl = MyConstant.IMAGE_ADDRESS + imageUrl;
            Log.i(TAG,picUrl);
            ImageLoader.getInstance().displayImage(picUrl, imvSosPerson, MyApplication.optionsForPerson);


            int sex = sosPerson.getSex();
            String strSex = "男";
            if(sex == 1)
            {
                strSex = "男";
            }
            else
            {
                strSex = "女";
            }

            tvSosSex.setText(strSex);
            tvSosAge.setText(String.valueOf(sosPerson.getAge()));
            tvSosPhone.setText(sosPerson.getMobile());
            tvSosPersonAddress.setText(sosPerson.getAddress());
            tvSosName.setText(sosPerson.getName());

            //紧急联系人
            ArrayList<EmergencyInfo> emergencyInfos = sosPerson.getEmergency();

            int emergencySize = emergencyInfos.size();
            if(emergencySize == 0)
            {
                return;
            }
            else if(emergencySize == 1)
            {
                this.findViewById(R.id.emergencyLayout1).setVisibility(View.VISIBLE);  //默认是GONE

            }
            else if(emergencySize == 2)
            {
                this.findViewById(R.id.emergencyLayout1).setVisibility(View.VISIBLE);
                this.findViewById(R.id.emergencyLayout2).setVisibility(View.VISIBLE);

            }

            for(int i = 0; i < emergencyInfos.size(); i++) {

                EmergencyInfo emergencyInfo = emergencyInfos.get(i);
                if (i == 0)
                {
                    tvEmergencyName1.setText(emergencyInfo.getName());
                    tvEmergencyMobile1.setText(emergencyInfo.getMobile());
                    tvAddress1.setText(emergencyInfo.getAddress());
                }
                else
                {
                    tvEmergencyName2.setText(emergencyInfo.getName());
                    tvEmergencyMobile2.setText(emergencyInfo.getMobile());
                    tvAddress2.setText(emergencyInfo.getAddress());
                }
            }
        }
        return;
    }
    /**
     * 获取救援详情数据
     */
    private void initData() {

        User user = new User(RescueDetailActivity.this);
        String token = user.getUserToken();
        Log.i(TAG,"url ="+ MyConstant.API_RESCUE_DETAIL + "token="+token+",orderId="+mOrderId);
        OkHttpUtils
                .get(MyConstant.API_RESCUE_DETAIL)//
                .tag(MyConstant.API_RESCUE_DETAIL)//
                .params("tokenId", token)//
                .params("orderId",mOrderId)
                .execute(new StringDialogCallback((Activity) RescueDetailActivity.this) {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {

                        int retCode = response.code();
                        if(retCode == 200)
                        {
                            Log.i(TAG,"onSuccess 200 return："+s);
                            try
                            {
                                MyRescueDetailJson myRescueDetailJson = new Gson().fromJson(s,MyRescueDetailJson.class);

                                if(myRescueDetailJson.getStatus() == 1)//确认成功
                                {
                                   dealSuccess(myRescueDetailJson.getData());
                                }
                                else {
                                    String msg = myRescueDetailJson.getMsg();
                                    MyErrorDeal.dealError(msg,RescueDetailActivity.this);
                                }
                            }
                            catch (Exception e)
                            {
                                e.printStackTrace();
                            }
                        }
                        else
                        {
                            MyErrorDeal.dealHttpError(response,RescueDetailActivity.this);
                            Log.i(TAG,"onSuccess "+retCode+"return: "+s);
                        }
                    }

                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {

                        MyErrorDeal.dealHttpError(response,RescueDetailActivity.this);

                    }
                });
        return;
    }

    public void getLastIntent()
    {
        Intent i = this.getIntent();
        if(i != null)
        {
            Bundle b = i.getExtras();
            if(b != null)
            {
                mOrderId = b.getString("orderId");
            }
        }

        return;
    }


    private void initView() {
        getLastIntent();
        //标题
        TextView tvTitle = (TextView) this.findViewById(R.id.tv_title);
        tvTitle.setText("救援详情");
        ImageButton btImgLeft = (ImageButton) this.findViewById(R.id.imgBtLeft);
        btImgLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RescueDetailActivity.this.finish();
            }
        });

        //志愿者信息
         tvVolunteerName = (TextView) this.findViewById(R.id.tvVolunteerName);
         tvRescueAddress = (TextView) this.findViewById(R.id.tvRescueAddress);
         tvSosTime = (TextView) this.findViewById(R.id.tvSosTime);
         tvRescueTime = (TextView) this.findViewById(R.id.tvRescueTime);
         tvSosName0 = (TextView) this.findViewById(R.id.tvSosName0);

        //求救人信息
         tvSosName = (TextView) this.findViewById(R.id.tvSosName);
         tvSosSex = (TextView) this.findViewById(R.id.tvSosSex);
         tvSosAge = (TextView) this.findViewById(R.id.tvSosAge);
         tvSosPhone = (TextView) this.findViewById(R.id.tvSosPhone);
         tvSosPersonAddress = (TextView) this.findViewById(R.id.tvSosPersonAddress);  //求救人住址
          imvSosPerson = (ImageView) this.findViewById(R.id.imvSosPerson);  //求救人照片

        //紧急联系人1
         tvEmergencyName1 = (TextView) this.findViewById(R.id.tvEmergencyName1);
         tvEmergencyMobile1 = (TextView) this.findViewById(R.id.tvEmergencyMobile1);
         tvAddress1 = (TextView) this.findViewById(R.id.tvAddress1);

        //紧急联系人2
         tvEmergencyName2 = (TextView) this.findViewById(R.id.tvEmergencyName2);
         tvEmergencyMobile2 = (TextView) this.findViewById(R.id.tvEmergencyMobile2);
         tvAddress2 = (TextView) this.findViewById(R.id.tvAddress2);

    }
}
